<template>
  <div>
    <!-- 分页查询 -->
    <el-row>
      <el-col :span="2">
        <el-button
          type="primary"
          @click="dialogFormVisibleAdd = true"
          plain
          style="width: 90%"
          >新增</el-button
        >
      </el-col>
      <el-col :span="4">
        <el-input
          placeholder="标题/关键词"
          type="text"
          style="width: 100%"
          v-model="name"
        />
      </el-col>
      <el-col :span="2">
        <el-button
          type="primary"
          style="width: 80%; margin-left: 16px"
          icon="el-icon-search"
          @click="page()"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="18"></el-col>
    </el-row>
    <hr />
    <el-table :data="list" border style="width: 100%;height=90%">
      <el-table-column prop="schemetittle" label="标题"> </el-table-column>
      <el-table-column prop="modelid" label="模板关键字"> </el-table-column>
      <el-table-column prop="schemetarget" label="方案目标"> </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="loadUP(scope.$index, scope.row)"
            type="primary"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="loaddelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="num"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- add -->
    <el-dialog title="新增模版" :visible.sync="dialogFormVisibleAdd" id="add">
      <el-tabs v-model="addmenu" type="border-card" style="margin-top: -20px">
        <el-tab-pane name="1" label="基本信息">
          <div style="margin-left: 50px; margin-top: 20px">
            <el-row>
              <el-col :span="3" style="text-align: auto"
                ><strong>方案标题:</strong></el-col
              >
              <el-col :span="5">
                <el-input
                  type="text"
                  v-model="addform.scheme.schemetittle"
                  size="small"
                  style="margin-top: -10px"
              /></el-col>
              <el-col :span="3" style="text-align: auto">.</el-col>

              <el-col :span="3" style="text-align: auto"
                ><strong>选用模版:</strong></el-col
              >
              <el-col :span="5">
                <el-select
                  v-model="addform.scheme.modelid"
                  size="small"
                  style="margin-top: -10px"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in models"
                    :key="item"
                    :label="item"
                    :value="item"
                  >
                  </el-option> </el-select
              ></el-col>
              <el-col :span="5" style="text-align: auto">.</el-col>
            </el-row>

            <br />
            <el-row>
              <el-col :span="3" style="text-align: auto"
                ><strong>开始时间:</strong></el-col
              >
              <el-col :span="5">
                <el-date-picker
                  type="date"
                  v-model="addform.scheme.starttime"
                  size="small"
                  style="margin-top: -10px"
                  placeholder="开始时间"
                >
                </el-date-picker
              ></el-col>
              <el-col :span="3" style="text-align: auto">.</el-col>

              <el-col :span="3" style="text-align: auto"
                ><strong>结束时间:</strong></el-col
              >
              <el-col :span="5">
                <el-date-picker
                  type="date"
                  v-model="addform.scheme.endtime"
                  size="small"
                  style="margin-top: -10px"
                  placeholder="结束时间"
                >
                </el-date-picker
              ></el-col>
              <el-col :span="5" style="text-align: auto">.</el-col>
            </el-row>

            <br />
            <el-row>
              <el-col :span="3" style="text-align: auto"
                ><strong>方案目标:</strong></el-col
              >
              <el-col :span="18">
                <el-input
                  type="textarea"
                  v-model="addform.scheme.schemetarget"
                  size="small"
              /></el-col>
              <el-col :span="3" style="text-align: auto">.</el-col>
            </el-row>
            <br />
          </div>
        </el-tab-pane>

        <el-tab-pane name="2" label="饮食干预">
          <el-row style="text-align: right">
            <el-col>
              <el-popover placement="right" width="400" trigger="click">
                <el-row>
                  <el-col :span="12">
                    <el-input
                      v-model="meals.name"
                      size="small "
                      placeholder="食物名称"
                    ></el-input>
                  </el-col>
                  <el-col :span="12">
                    <el-button
                      style="margin-left: 10px"
                      type="success"
                      size="small "
                      @click="mpage()"
                    >
                      查询</el-button
                    >
                  </el-col>
                </el-row>
                <el-table :data="meals.list">
                  <el-table-column
                    width="150"
                    property="name"
                    label="食品名称"
                  ></el-table-column>
                  <el-table-column
                    width="100"
                    property="typeid"
                    label="所属类别"
                  ></el-table-column>
                  <el-table-column width="100" label="操作">
                    <template slot-scope="scope">
                      <el-button
                        type="success"
                        @click="addjia(scope.$index, scope.row)"
                        size="mini"
                        >+</el-button
                      >
                    </template>
                  </el-table-column>
                </el-table>
                <el-pagination
                  @size-change="mhandleSizeChange"
                  @current-change="mhandleCurrentChange"
                  :current-page="meals.num"
                  :page-sizes="[10, 20, 50, 100]"
                  :page-size="meals.size"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="meals.total"
                >
                </el-pagination>

                <el-button
                  type="primary"
                  @click="mpage()"
                  plain
                  slot="reference"
                  >添加食物</el-button
                >
              </el-popover>
            </el-col>
          </el-row>

          <hr />
          <el-table :data="addform.food" style="width: 100%">
            <el-table-column prop="eattimes" label="" width="100">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="danger"
                  style="width: 20px"
                  @click="addjian(scope.$index, scope.row)"
                  >-
                </el-button>
              </template>
            </el-table-column>
            <el-table-column prop="recommendfood" label="食物名称">
            </el-table-column>
            <el-table-column prop="foodquantity" label="份量/餐">
              <template slot-scope="scope">
                <el-button size="mini" @click="fjian(scope.$index, scope.row)"
                  >-</el-button
                >
                <el-button size="mini" style="border: none"
                  ><strong>{{ scope.row.foodquantity }}</strong>
                </el-button>
                <el-button size="mini" @click="fjia(scope.$index, scope.row)"
                  >+</el-button
                >
              </template>
            </el-table-column>
            <el-table-column prop="eattimes" label="餐数/天">
              <template slot-scope="scope">
                <el-button size="mini" @click="cjian(scope.$index, scope.row)"
                  >-</el-button
                >
                <el-button size="mini" style="border: none"
                  ><strong>{{ scope.row.eattimes }}</strong>
                </el-button>
                <el-button size="mini" @click="cjia(scope.$index, scope.row)"
                  >+</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>

        <el-tab-pane name="3" label="运动干预">
          <el-row style="text-align: right">
            <el-col>
              <el-popover placement="right" width="400" trigger="click">
                <el-row>
                  <el-col :span="12">
                    <el-input
                      v-model="sportsItemLibrary.name"
                      size="small "
                      placeholder="运动名称"
                    ></el-input>
                  </el-col>
                  <el-col :span="12">
                    <el-button
                      style="margin-left: 10px"
                      type="success"
                      size="small "
                      @click="spage()"
                    >
                      查询</el-button
                    >
                  </el-col>
                </el-row>
                <el-table :data="sportsItemLibrary.list">
                  <el-table-column
                    property="name"
                    label="项目名称"
                  ></el-table-column>
                  <el-table-column
                    property="strength"
                    label="项目强度"
                  ></el-table-column>
                  <el-table-column label="适用性别">
                    <template slot-scope="scope">
                      {{
                        scope.row.applytogender == 0
                          ? "男"
                          : scope.row.applytogender == 1
                          ? "女"
                          : "不限"
                      }}
                    </template>
                  </el-table-column>
                  <el-table-column label="操作">
                    <template slot-scope="scope">
                      <el-button
                        type="success"
                        @click="addjias(scope.$index, scope.row)"
                        size="mini"
                        >+</el-button
                      >
                    </template>
                  </el-table-column>
                </el-table>
                <el-pagination
                  @size-change="shandleSizeChange"
                  @current-change="shandleCurrentChange"
                  :current-page="sportsItemLibrary.num"
                  :page-sizes="[10, 20, 50, 100]"
                  :page-size="sportsItemLibrary.size"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="sportsItemLibrary.total"
                >
                </el-pagination>

                <el-button
                  type="primary"
                  @click="spage()"
                  plain
                  slot="reference"
                  >添加项目</el-button
                >
              </el-popover>
            </el-col>
          </el-row>

          <hr />
          <el-table :data="addform.sprot" style="width: 100%">
            <el-table-column prop="eattimes" label="" width="100">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="danger"
                  style="width: 20px"
                  @click="addjians(scope.$index, scope.row)"
                  >-
                </el-button>
              </template>
            </el-table-column>
            <el-table-column prop="sportname" label="项目名称">
            </el-table-column>
            <el-table-column prop="sporttime" label="运动时长（分钟）">
              <template slot-scope="scope">
                <el-row>
                  <el-col :span="6"
                    ><el-input size="mini" v-model="scope.row.sporttime"
                  /></el-col>
                  <el-col :span="18">/分钟</el-col>
                </el-row>
              </template>
            </el-table-column>
            <el-table-column prop="sportcycle" label="运动周期">
              <template slot-scope="scope">
                <el-input
                  size="mini"
                  v-model="scope.row.sportcycle"
                  style="width: 50%"
                />
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
      <br />
      <el-row style="text-align: right">
        <el-col :span="18">.</el-col>
        <el-col :span="5">
          <el-button type="primary" plain @click="dialogFormVisibleAdd = false"
            >取消</el-button
          >
          <el-button
            style="margin-left: 20px; width: 40%"
            type="primary"
            @click="add()"
            >提交</el-button
          >
        </el-col>
        <el-col :span="1"></el-col>
      </el-row>
    </el-dialog>

    <!-- update -->
    <el-dialog
      title="修改模版"
      :visible.sync="dialogFormVisibleUpdate"
      id="add"
    >
      <el-tabs v-model="upmenu" type="border-card" style="margin-top: -20px">
        <el-tab-pane name="1" label="基本信息">
          <div style="margin-left: 50px; margin-top: 20px">
            <el-row>
              <el-col :span="3" style="text-align: auto"
                ><strong>方案标题:</strong></el-col
              >
              <el-col :span="5">
                <el-input
                  type="text"
                  v-model="upform.scheme.schemetittle"
                  size="small"
                  style="margin-top: -10px"
              /></el-col>
              <el-col :span="3" style="text-align: auto">.</el-col>

              <el-col :span="3" style="text-align: auto"
                ><strong>选用模版:</strong></el-col
              >
              <el-col :span="5">
                <el-select
                  v-model="upform.scheme.modelid"
                  size="small"
                  style="margin-top: -10px"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in models"
                    :key="item"
                    :label="item"
                    :value="item"
                  >
                  </el-option> </el-select
              ></el-col>
              <el-col :span="5" style="text-align: auto">.</el-col>
            </el-row>

            <br />
            <el-row>
              <el-col :span="3" style="text-align: auto"
                ><strong>开始时间:</strong></el-col
              >
              <el-col :span="5">
                <el-date-picker
                  type="date"
                  v-model="upform.scheme.starttime"
                  size="small"
                  style="margin-top: -10px"
                  placeholder="开始时间"
                >
                </el-date-picker
              ></el-col>
              <el-col :span="3" style="text-align: auto">.</el-col>

              <el-col :span="3" style="text-align: auto"
                ><strong>结束时间:</strong></el-col
              >
              <el-col :span="5">
                <el-date-picker
                  type="date"
                  v-model="upform.scheme.endtime"
                  size="small"
                  style="margin-top: -10px"
                  placeholder="结束时间"
                >
                </el-date-picker
              ></el-col>
              <el-col :span="5" style="text-align: auto">.</el-col>
            </el-row>

            <br />
            <el-row>
              <el-col :span="3" style="text-align: auto"
                ><strong>方案目标:</strong></el-col
              >
              <el-col :span="18">
                <el-input
                  type="textarea"
                  v-model="upform.scheme.schemetarget"
                  size="small"
              /></el-col>
              <el-col :span="3" style="text-align: auto">.</el-col>
            </el-row>
            <br />
          </div>
        </el-tab-pane>

        <el-tab-pane name="2" label="饮食干预">
          <el-row style="text-align: right">
            <el-col>
              <el-popover placement="right" width="400" trigger="click">
                <el-row>
                  <el-col :span="12">
                    <el-input
                      v-model="meals.name"
                      size="small "
                      placeholder="食物名称"
                    ></el-input>
                  </el-col>
                  <el-col :span="12">
                    <el-button
                      style="margin-left: 10px"
                      type="success"
                      size="small "
                      @click="mpage()"
                    >
                      查询</el-button
                    >
                  </el-col>
                </el-row>
                <el-table :data="meals.list">
                  <el-table-column
                    width="150"
                    property="name"
                    label="食品名称"
                  ></el-table-column>
                  <el-table-column
                    width="100"
                    property="typeid"
                    label="所属类别"
                  ></el-table-column>
                  <el-table-column width="100" label="操作">
                    <template slot-scope="scope">
                      <el-button
                        type="success"
                        @click="upjia(scope.$index, scope.row)"
                        size="mini"
                        >+</el-button
                      >
                    </template>
                  </el-table-column>
                </el-table>
                <el-pagination
                  @size-change="mhandleSizeChange"
                  @current-change="mhandleCurrentChange"
                  :current-page="meals.num"
                  :page-sizes="[10, 20, 50, 100]"
                  :page-size="meals.size"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="meals.total"
                >
                </el-pagination>

                <el-button
                  type="primary"
                  @click="mpage()"
                  plain
                  slot="reference"
                  >添加食物</el-button
                >
              </el-popover>
            </el-col>
          </el-row>

          <hr />
          <el-table :data="upform.scheme.foods" style="width: 100%">
            <el-table-column prop="eattimes" label="" width="100">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="danger"
                  style="width: 20px"
                  @click="upjian(scope.$index, scope.row)"
                  >-
                </el-button>
              </template>
            </el-table-column>
            <el-table-column prop="recommendfood" label="食物名称">
            </el-table-column>
            <el-table-column prop="foodquantity" label="份量/餐">
              <template slot-scope="scope">
                <el-button size="mini" @click="fjianUP(scope.$index, scope.row)"
                  >-</el-button
                >
                <el-button size="mini" style="border: none"
                  ><strong>{{ scope.row.foodquantity }}</strong>
                </el-button>
                <el-button size="mini" @click="fjiaUP(scope.$index, scope.row)"
                  >+</el-button
                >
              </template>
            </el-table-column>
            <el-table-column prop="eattimes" label="餐数/天">
              <template slot-scope="scope">
                <el-button size="mini" @click="cjianUP(scope.$index, scope.row)"
                  >-</el-button
                >
                <el-button size="mini" style="border: none"
                  ><strong>{{ scope.row.eattimes }}</strong>
                </el-button>
                <el-button size="mini" @click="cjiaUP(scope.$index, scope.row)"
                  >+</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>

        <el-tab-pane name="3" label="运动干预">
          <el-row style="text-align: right">
            <el-col>
              <el-popover placement="right" width="400" trigger="click">
                <el-row>
                  <el-col :span="12">
                    <el-input
                      v-model="sportsItemLibrary.name"
                      size="small "
                      placeholder="运动名称"
                    ></el-input>
                  </el-col>
                  <el-col :span="12">
                    <el-button
                      style="margin-left: 10px"
                      type="success"
                      size="small "
                      @click="spage()"
                    >
                      查询</el-button
                    >
                  </el-col>
                </el-row>
                <el-table :data="sportsItemLibrary.list">
                  <el-table-column
                    property="name"
                    label="项目名称"
                  ></el-table-column>
                  <el-table-column
                    property="strength"
                    label="项目强度"
                  ></el-table-column>
                  <el-table-column label="适用性别">
                    <template slot-scope="scope">
                      {{
                        scope.row.applytogender == 0
                          ? "男"
                          : scope.row.applytogender == 1
                          ? "女"
                          : "不限"
                      }}
                    </template>
                  </el-table-column>
                  <el-table-column label="操作">
                    <template slot-scope="scope">
                      <el-button
                        type="success"
                        @click="upjias(scope.$index, scope.row)"
                        size="mini"
                        >+</el-button
                      >
                    </template>
                  </el-table-column>
                </el-table>
                <el-pagination
                  @size-change="shandleSizeChange"
                  @current-change="shandleCurrentChange"
                  :current-page="sportsItemLibrary.num"
                  :page-sizes="[10, 20, 50, 100]"
                  :page-size="sportsItemLibrary.size"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="sportsItemLibrary.total"
                >
                </el-pagination>

                <el-button
                  type="primary"
                  @click="spage()"
                  plain
                  slot="reference"
                  >添加项目</el-button
                >
              </el-popover>
            </el-col>
          </el-row>

          <hr />
          <el-table :data="upform.scheme.sprots" style="width: 100%">
            <el-table-column prop="eattimes" label="" width="100">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="danger"
                  style="width: 20px"
                  @click="upjians(scope.$index, scope.row)"
                  >-
                </el-button>
              </template>
            </el-table-column>
            <el-table-column prop="sportname" label="项目名称">
            </el-table-column>
            <el-table-column prop="sporttime" label="运动时长（分钟）">
              <template slot-scope="scope">
                <el-row>
                  <el-col :span="10"
                    ><el-input size="mini" v-model="scope.row.sporttime"
                  /></el-col>
                  <el-col :span="14">/分钟</el-col>
                </el-row>
              </template>
            </el-table-column>
            <el-table-column prop="sportcycle" label="运动周期">
              <template slot-scope="scope">
                <el-input
                  size="mini"
                  v-model="scope.row.sportcycle"
                  style="width: 50%"
                />
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
      <br />
      <el-row style="text-align: right">
        <el-col :span="18">.</el-col>
        <el-col :span="5">
          <el-button
            type="primary"
            plain
            @click="dialogFormVisibleUpdate = false"
            >取消</el-button
          >
          <el-button
            style="margin-left: 20px; width: 40%"
            type="primary"
            @click="update()"
            >提交</el-button
          >
        </el-col>
        <el-col :span="1"></el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 分页查询
      num: 1,
      size: 10,
      total: 2,
      list: [],
      name: "",

      // add
      dialogFormVisibleAdd: false,
      addmenu: "1", // 页面默认数据
      models: [
        // 选用模版
        "阳虚",
        "阴虚",
        "气虚",
        "强迫症",
        "焦虑症",
        "恐怖化",
        "糖尿病",
        "哮喘",
        "肥胖症",
        "老年高血压",
        "高血压",
        "高血糖",
      ],
      // 新增数据
      addform: {
        scheme: {
          // 基础数据
          schemetittle: "",
          modelid: "",
          starttime: "",
          endtime: "",
          schemetarget: "",
          foods: [],
          sprots: [],
        },
        food: [
          // add饮食方案
        ],
        sprot: [
          // add运动干预方案
        ],
      },

      // 饮食查询
      meals: {
        num: 1,
        size: 10,
        total: 2,
        list: [],
        name: "",
      },
      // 运动查询
      sportsItemLibrary: {
        num: 1,
        size: 10,
        total: 2,
        list: [],
        name: "",
      },

      // update
      dialogFormVisibleUpdate: false,
      upmenu: "1", // 页面默认数据
      upform: {
        scheme: {
          // 基础数据
          schemetittle: "",
          modelid: "",
          starttime: "",
          endtime: "",
          schemetarget: "",
          foods: [],
          sprots: [],
        },
      },
    };
  },
  methods: {
    // 分页查询
    handleSizeChange(val) {
      this.size = val;
      this.page();
    },
    handleCurrentChange(val) {
      this.num = val;
      this.page();
    },
    page() {
      const loading = this.$loading({
        lock: true,
        text: "正在加载数据中...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      this.axios
        .get("/rear/Scheme/page", {
          params: { num: this.num, size: this.size, name: this.name.trim() },
        })
        .then((res) => {
          this.list = res.data.list;
          this.total = res.data.total;
          loading.close();
        })
        .catch((res) => {
          this.$message.error("服务器出现问题，请联系管理员！");
          loading.close();
        });
    },

    // add
    // 饮食
    mpage() {
      this.axios
        .get("/rear/Scheme/mpage", {
          params: {
            num: this.meals.num,
            size: this.meals.size,
            name: this.meals.name.trim(),
          },
        })
        .then((res) => {
          this.meals.list = res.data.list;
          this.meals.total = res.data.total;
        })
        .catch((res) => {
          this.$message.error("服务器出现问题，请联系管理员！");
        });
    },
    mhandleSizeChange(val) {
      this.meals.size = val;
      this.mpage();
    },
    mhandleCurrentChange(val) {
      this.meals.size = val;
      this.mpage();
    },
    addjia(index, row) {
      let food = { recommendfood: row.name, foodquantity: 1, eattimes: 1 };
      let bol = true;
      this.addform.food.forEach((element) => {
        if (element.recommendfood == food.recommendfood) {
          bol = false;
        }
      });
      if (bol) {
        this.addform.food.push(food);
      }
    },
    addjian(index, row) {
      this.addform.food.splice(index, 1);
    },
    fjia(index, row) {
      if (row.foodquantity == 15) {
        return;
      }
      this.addform.food[index].foodquantity = row.foodquantity + 1;
    },
    fjian(index, row) {
      if (row.foodquantity == 1) {
        return;
      }
      this.addform.food[index].foodquantity = row.foodquantity - 1;
    },
    cjia(index, row) {
      if (row.eattimes == 5) {
        return;
      }
      this.addform.food[index].eattimes = row.eattimes + 1;
    },
    cjian(index, row) {
      if (row.eattimes == 1) {
        return;
      }
      this.addform.food[index].eattimes = row.eattimes - 1;
    },
    // 运动
    spage() {
      this.axios
        .get("/rear/Scheme/spage", {
          params: {
            num: this.sportsItemLibrary.num,
            size: this.sportsItemLibrary.size,
            name: this.sportsItemLibrary.name.trim(),
          },
        })
        .then((res) => {
          this.sportsItemLibrary.list = res.data.list;
          this.sportsItemLibrary.total = res.data.total;
        })
        .catch((res) => {
          this.$message.error("服务器出现问题，请联系管理员！");
        });
    },
    shandleSizeChange(val) {
      this.sportsItemLibrary.size = val;
      this.spage();
    },
    shandleCurrentChange(val) {
      this.sportsItemLibrary.size = val;
      this.spage();
    },
    addjias(index, row) {
      let sprot = {
        sportname: row.name,
        sporttime: 1,
        sportcycle: "1 周",
      };
      let bol = true;
      this.addform.sprot.forEach((element) => {
        if (element.sportname == sprot.sportname) {
          bol = false;
        }
      });
      if (bol) {
        this.addform.sprot.push(sprot);
      }
    },
    addjians(index, row) {
      this.addform.sprot.splice(index, 1);
    },
    add() {
      if (this.addform.scheme.modelid == "") {
        this.$message({
          message: "请选择模版 ！",
          type: "warning",
        });
        return;
      }
      let bol = true;
      this.addform.sprot.forEach((element) => {
        if (isNaN(element.sporttime)) {
          bol = false;
        }
      });
      if (!bol) {
        this.$message({
          message: "运动干预的运动的时长'类型异常' ！",
          type: "warning",
        });
        return;
      }
      const loading = this.$loading({
        lock: true,
        text: "正在拼命提交中...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      let params = this.addform.scheme;
      params.foods = this.addform.food;
      params.sprots = this.addform.sprot;
      this.axios
        .post("/rear/Scheme/add", params)
        .then((res) => {
          if (res.data) {
            this.$message({
              message: "提交成功！",
              type: "success",
            });
            this.addform = {
              scheme: {
                schemetittle: "",
                modelid: "",
                starttime: "",
                endtime: "",
                schemetarget: "",
                foods: [],
                sprots: [],
              },
              food: [],
              sprot: [],
            };
          }
          this.dialogFormVisibleAdd = false;
          loading.close();
          this.page();
        })
        .catch((res) => {
          this.$message.error("服务器出现问题，请联系管理员！");
          this.dialogFormVisibleAdd = false;
          loading.close();
        });
    },

    // update
    loadUP(index, row) {
      this.dialogFormVisibleUpdate = true;
      this.upform.scheme = row;
    },
    upjia(index, row) {
      let food = { recommendfood: row.name, foodquantity: 1, eattimes: 1 };
      let bol = true;
      this.upform.scheme.foods.forEach((element) => {
        if (element.recommendfood == food.recommendfood) {
          bol = false;
        }
      });
      if (bol) {
        this.upform.scheme.foods.push(food);
      }
    },
    upjian(index, row) {
      this.upform.scheme.foods.splice(index, 1);
    },
    upjias(index, row) {
      let sprot = {
        sportname: row.name,
        sporttime: 1,
        sportcycle: "1 周",
      };
      let bol = true;
      this.upform.scheme.sprots.forEach((element) => {
        if (element.sportname == sprot.sportname) {
          bol = false;
        }
      });
      if (bol) {
        this.upform.scheme.sprots.push(sprot);
      }
    },
    upjians(index, row) {
      this.upform.scheme.sprots.splice(index, 1);
    },
    fjiaUP(index, row) {
      if (row.foodquantity == 15) {
        return;
      }
      this.upform.scheme.foods[index].foodquantity = row.foodquantity + 1;
    },
    fjianUP(index, row) {
      if (row.foodquantity == 1) {
        return;
      }
      this.upform.scheme.foods[index].foodquantity = row.foodquantity - 1;
    },
    cjiaUP(index, row) {
      if (row.eattimes == 5) {
        return;
      }
      this.upform.scheme.foods[index].eattimes = row.eattimes + 1;
    },
    cjianUP(index, row) {
      if (row.eattimes == 1) {
        return;
      }
      this.upform.scheme.foods[index].eattimes = row.eattimes - 1;
    },
    update() {
      if (this.upform.scheme.modelid == "") {
        this.$message({
          message: "请选择模版 ！",
          type: "warning",
        });
        return;
      }
      let bol = true;
      this.upform.scheme.sprots.forEach((element) => {
        if (isNaN(element.sporttime)) {
          bol = false;
        }
      });
      if (!bol) {
        this.$message({
          message: "运动干预的运动的时长'类型异常' ！",
          type: "warning",
        });
        return;
      }
      const loading = this.$loading({
        lock: true,
        text: "正在拼命提交中...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      this.axios
        .post("/rear/Scheme/update", this.upform.scheme)
        .then((res) => {
          if (res.data) {
            this.$message({
              message: "提交成功！",
              type: "success",
            });
          }
          this.dialogFormVisibleUpdate = false;
          loading.close();
          this.page();
        })
        .catch((res) => {
          this.$message.error("服务器出现问题，请联系管理员！");
          this.dialogFormVisibleUpdate = false;
          loading.close();
        });
    },

    // 删除数据
    loaddelete(index, row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios
            .get("/rear/Scheme/delete", { params: { id: row.id } })
            .then((res) => {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.page();
            })
            .catch((res) => {
              this.$message.error("服务器出现问题，请联系管理员！");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    this.page();
  },
};
</script>

<style scoped>
#add {
  text-align: left;
}
</style>